package com.leesche.zcsp.node.web.controller.template;

import com.leesche.zcsp.node.app.v1.timer.TokenCacheTimer;
import com.leesche.zcsp.node.utils.web.SessionManager;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.model.SysUser;
import com.leesche.zcsp.node.web.service.SysModuleService;
import com.leesche.zcsp.node.web.service.impl.data.read.QuerySysModuleCondition;
import com.leesche.zcsp.node.web.vo.Principal;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Jerry Chan
 * @version V1.0
 * @Title: node
 * @Package com.leesche.zcsp.node.web.controller.template
 * @Description:( 访问模版类：UserTemplate和LoginTemplate的公共类 )
 * @date 2016年10月19日 10:56
 */
public class AccessTemplate {

    @Autowired
    protected SysModuleService sysModuleService;

    /**
     * @param loginType 登录的类型
     * @param loginUser 登录的用户
     * @return
     * @Title: savePrincipalLoginState(Integer loginType, SysUser sysUser)
     * @Description: TODO(封装当前登录用户的信息，并设置为登录状态)
     * @author Jerry
     * @date 2016-10-19 11:08:20
     */
    protected void savePrincipalLoginState(Integer loginType, SysUser loginUser) {

        String principalName = loginUser.getBusinessno() != null ? loginUser
                .getBusinessno() : loginUser.getUsername();

        Principal principal = new Principal(loginUser.getUserid(),
                loginUser.getPuserid(), principalName,
                loginUser.getBusinessno(), loginUser.getNtype(),
                loginUser.getOpenid(), loginType,loginUser.getShortcode());

        QuerySysModuleCondition querySysModuleCondition = new QuerySysModuleCondition();
        querySysModuleCondition.setnType(loginType);
        querySysModuleCondition.setUserId(principal.getUserid());
        principal.setModuleIds(sysModuleService.findModuleIDbyUserID(querySysModuleCondition));

        SessionManager.setSessionAttribute(Constants.LOGIN_CURRENT_USER, principal);
    }

    /**
     * @param loginType 登录的类型
     * @param loginUser 登录的用户
     * @return
     * @Title: savePrincipalLoginState(Integer loginType, SysUser sysUser)
     * @Description: TODO(封装当前登录用户的信息，并设置为登录状态)
     * @author Jerry
     * @date 2016-10-19 11:08:20
     */
    protected void savePrincipalLoginState(Integer loginType, SysUser loginUser,String token) {

        String principalName = loginUser.getBusinessno() != null ? loginUser
                .getBusinessno() : loginUser.getUsername();

        Principal principal = new Principal(loginUser.getUserid(),
                loginUser.getPuserid(), principalName,
                loginUser.getBusinessno(), loginUser.getNtype(),
                loginUser.getOpenid(),token, loginType,loginUser.getShortcode());

        QuerySysModuleCondition querySysModuleCondition = new QuerySysModuleCondition();
        querySysModuleCondition.setnType(loginType);
        querySysModuleCondition.setUserId(principal.getUserid());
        principal.setModuleIds(sysModuleService.findModuleIDbyUserID(querySysModuleCondition));

        //增加用户权限到缓存中去
        TokenCacheTimer.addUserPrincipal(token,principal);
/*
        SessionManager.setSessionAttribute(Constants.LOGIN_CURRENT_USER, principal);
*/
    }

}
